/*
 * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved.
 * <p>
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/lgpl.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package top.continew.starter.security.crypto.encryptor;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.security.crypto.password.PasswordEncoder;
import top.continew.starter.security.crypto.autoconfigure.CryptoContext;
import top.continew.starter.security.crypto.autoconfigure.CryptoProperties;
import top.continew.starter.security.crypto.autoconfigure.PasswordEncoderProperties;

/**
 * 密码编码器加/解密处理器
 *
 * <p>
 * 使用前必须注入 {@link PasswordEncoder}，此加密方式不可逆，适合于密码场景
 * </p>
 *
 * @see PasswordEncoder
 * @see PasswordEncoderProperties
 *
 * @author Charles7c
 * @since 2.13.3
 */
public class PasswordEncoderEncryptor extends AbstractEncryptor {

    private final PasswordEncoder passwordEncoder = SpringUtil.getBean(PasswordEncoder.class);
    private final CryptoProperties properties = SpringUtil.getBean(CryptoProperties.class);

    public PasswordEncoderEncryptor(CryptoContext context) {
        super(context);
    }

    @Override
    public String encrypt(String plaintext) {
        // 如果已经是加密格式，直接返回
        if (properties.getPasswordEncoder().getAlgorithm().getPattern().matcher(plaintext).matches()) {
            return plaintext;
        }
        return passwordEncoder.encode(plaintext);
    }

    @Override
    public String decrypt(String ciphertext) {
        return ciphertext;
    }
}
